import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  DelReq,
} from '@fast-crud/fast-crud';

import type { Ref } from 'vue';

import type { SopApiInfoModel } from '#/api/devOperation/sop/model/sopApiInfoModel';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { SopApiInfoApi } from '#/api/devOperation/sop/sopApiInfo';
import { SopPermGroupPermissionApi } from '#/api/devOperation/sop/sopPermGroupPermission';
import { DictEnum } from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
  STATE_NUMBER_CONSTANT_DICT,
  transformQuery,
  YES_NO_NUMBER_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.devOperation.sop.sopPermGroup;
type ContextRef = {
  groupIdRef: Ref<string>;
};

export function createCrudOptions(
  props: CreateCrudOptionsProps<SopApiInfoModel.SopApiInfoResultVO, ContextRef>,
): CreateCrudOptionsRet<SopApiInfoModel.SopApiInfoResultVO> {
  const selectedRowKeys = ref<string[]>([]);
  return {
    crudOptions: {
      request: {
        pageRequest: SopApiInfoApi.pageByGroupRequest,
        delRequest: async (ctx: DelReq) => {
          const { row } = ctx;
          return await SopPermGroupPermissionApi.deleteByGroupId({
            groupId: props.context.groupIdRef.value,
            apiIdList: [row.id],
          });
        },
        transformQuery: (query) => {
          const tq = transformQuery(query);
          tq.model.groupId = props.context.groupIdRef.value;
          return tq;
        },
      },
      actionbar: {
        buttons: {
          add: { show: false },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: async (ids: string[]) => {
              return await SopPermGroupPermissionApi.deleteByGroupId({
                groupId: props.context.groupIdRef.value,
                apiIdList: ids,
              });
            },
            role: permCode.authorize,
          }),
        },
      },
      form: { wrapper: { is: 'a-modal' } },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '120px',
        buttons: {
          edit: { show: false },
          view: { show: true },
          remove: { show: hasPermission(permCode.authorize) },
          copy: { show: false },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        application: {
          title: $t('devOperation.sop.sopApiInfo.application'),
          type: 'text',
          search: { show: true },
          editForm: { component: { disabled: true } },
          column: { width: 150 },
        },
        apiName: {
          title: $t('devOperation.sop.sopApiInfo.apiName'),
          type: 'text',
          editForm: { component: { disabled: true } },
          search: { show: true },
          column: { width: 150 },
        },
        apiVersion: {
          title: $t('devOperation.sop.sopApiInfo.apiVersion'),
          type: 'text',
          editForm: { component: { disabled: true } },
        },
        description: {
          title: $t('devOperation.sop.sopApiInfo.description'),
          type: 'text',
          editForm: { component: { disabled: true } },
        },
        remark: {
          title: $t('devOperation.sop.sopApiInfo.remark'),
          type: 'textarea',
        },
        interfaceClassName: {
          title: $t('devOperation.sop.sopApiInfo.interfaceClassName'),
          type: 'text',
          addForm: { show: false },
          editForm: { show: false },
          viewForm: { show: true },
          column: { show: false },
        },
        methodName: {
          title: $t('devOperation.sop.sopApiInfo.methodName'),
          type: 'text',
          addForm: { show: false },
          editForm: { show: false },
          viewForm: { show: true },
          column: { show: false },
        },
        paramInfo: {
          title: $t('devOperation.sop.sopApiInfo.paramInfo'),
          type: 'text',
          addForm: { show: false },
          editForm: { show: false },
          viewForm: { show: true },
          column: { show: false },
        },
        isNeedToken: {
          title: $t('devOperation.sop.sopApiInfo.isNeedToken'),
          type: 'dict-radio',
          dict: YES_NO_NUMBER_CONSTANT_DICT,
          addForm: { show: false },
          editForm: { show: false },
          viewForm: { show: true },
          column: { show: false },
        },
        hasCommonResponse: {
          title: $t('devOperation.sop.sopApiInfo.hasCommonResponse'),
          type: 'dict-radio',
          dict: YES_NO_NUMBER_CONSTANT_DICT,
          addForm: { show: false },
          editForm: { show: false },
          viewForm: { show: true },
          column: { show: false },
        },
        regSource: {
          title: $t('devOperation.sop.sopApiInfo.regSource'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.SOP_API_INFO_REG_SOURCE),
          column: { width: 100 },
        },
        apiMode: {
          title: $t('devOperation.sop.sopApiInfo.apiMode'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.SOP_API_INFO_API_MODE),
          addForm: { show: false },
          editForm: { show: false },
          viewForm: { show: true },
          column: { show: false },
        },
        status: {
          title: $t('devOperation.sop.sopApiInfo.status'),
          type: 'dict-radio',
          dict: STATE_NUMBER_CONSTANT_DICT,
          column: { width: 80 },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}
